#include "common.h"
#include <stdarg.h>

int err=0;
char * str_maker(const char * fmt,...){
    size_t size=32,n=strlen(fmt)+1;
    while(size<=n) size<<=1;
    va_list ap;
    char * buff=malloc(size);
    va_start(ap,fmt);
    int len=vsnprintf(buff,size,fmt,ap);
    while(len<=0||len>=size){
        free(buff);
        size<<=1;
        buff=malloc(size);
        va_start(ap,fmt);
        len=vsnprintf(buff,size,fmt,ap);
    }
    #ifdef DEBUG
    //puts(buff);
    #endif
    return buff;
}

unsigned hash(const char * ch){
    unsigned val=0,i;
    const unsigned size=0x3fff;
    for(;*ch;ch++){
        val=(val<<2)+*ch;
        if((i=val)&~size) val=(val^(i>>12))&size;
    }
    return val;
}

void print_error(char * type,int lineno,char * msg){
    err=1;
    printf("Error type %s at Line %d: %s.\n",type,lineno,msg);
    return;
}

unsigned new_var(){
    static unsigned o=0;
    return ++o;
}

unsigned new_label(){
    static unsigned o=0;
    return ++o;
}
